package com.itheima.controller;

import com.itheima.entity.ResultInfo;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultInfo login(@RequestBody User user) {
        User login = userService.login(user.getUsername(), user.getPassword());
        if (login != null) {
            session.setAttribute("user",login);
            return new ResultInfo(true,"登录成功");
        }else {
            return new ResultInfo(false,"登录失败,请重试");
        }
    }
    @RequestMapping("/logout")
    public  void  logout(){
        session.invalidate();
    }
}
